<?php
    include_once 'common.inc';
    $ok = 1;
    if ($gid !== $_POST['gid'])
        if (verifyGid($_POST['gid']) && isOwner($uid, $_POST['gid'])) {
            $gid = $_POST['gid'];
            $values = loadValues($gid);
            setCurrentGid($uid, $gid);
            getCurrentGid($uid, $game);
        } else
            $ok = 0;
    if ($ok) {
        $game['comments'] = $_POST['comments'];
        setGameOptions($gid, $game);
    }
    if ($ok && $_POST['to'] == 'swots') {
        $type = $_POST['type'];
        if (!is_numeric($type) || !($type >= 0 && $type < 4))
            $ok = 0;
        $count = $minCount;
        for ($i = 0; $ok && isset($_POST["caption_$i"]); $i++) {
            $values['swots'][$type][$i]['caption'] = $_POST["caption_$i"];
            if (!is_numeric($_POST["valuability_$i"]) || !($_POST["valuability_$i"] >= 0 && $_POST["valuability_$i"] < 5))
                $ok = 0;
            else
                $values['swots'][$type][$i]['valuability'] = $_POST["valuability_$i"];
            if ($_POST["caption_$i"] !== "")
                $count = $i + 1;
        }
        if ($ok) {
            if ($count > $maxCount)
                $count = $maxCount;
            if ($values['counts'][$type] < $count) {
                $values['counts'][$type] = $count;
                appendDefaultValues($values);
            } else
                $values['counts'][$type] = $count;
            $retPage = "swots.php?type=$type";
            $retBase = $type;
        }
    } else if ($ok && $_POST['to'] == 'relations') {
        $type = $_POST['type'];
        if (!is_numeric($type) || !($type >= 0 && $type < 2))
            $ok = 0;
        $index = $_POST['index'];
        if (!is_numeric($index) || !($index >= 0 && $index < $values['counts'][$type]))
            $ok = 0;
        if ($ok)
            for ($t = 2; $t < 4; $t++) {
                for ($i = 0; $ok && $i < $values['counts'][$t]; $i++) {
                    if (!is_numeric($_POST["valuability_$t" . "_$i"]) || !($_POST["valuability_$t" . "_$i"] >= 0 && $_POST["valuability_$t" . "_$i"] < 5))
                        $ok = 0;
                    else
                        $values['matrices'][$type * 2 + $t - 2][$index][$i] = $_POST["valuability_$t" . "_$i"];
                }
            }
        if ($ok) {
            $retPage = "relations.php?type=$type&index=$index";
            $retBase = 4 + ($type == 0 ? $index : $index + $values['counts'][0]);
        }
    }

    if ($ok) {
        setValues($gid, $values);

        $retCount = 4 + $values['counts'][0] + $values['counts'][1];
        $ok2 = 0;
        for ($ai = 1; $ai < $retCount; $ai++) {
            $i = ($retBase + $ai) % $retCount;
            if ($i < 4) $ok2 = hasNotDefaultSwots($values, $i);
            else $ok2 = hasNotDefaultRelations($values, ($i < 4 + $values['counts'][0] ? 0 : 1),
                    ($i < 4 + $values['counts'][0] ? $i - 4 : $i - 4 - $values['counts'][0]));
            if (!$ok2) {
                if ($i < 4)
                    $retPage = "swots.php?type=$i";
                else if ($i < 4 + $values['counts'][0])
                    $retPage = "relations.php?type=0&index=" . ($i - 4);
                else
                    $retPage = "relations.php?type=1&index=" . ($i - 4 - $values['counts'][0]);
                break;
            }
        }
        if ($ok2 && $retBase == $retCount - 1)
            $retPage = "result.php";
    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link href="swot.css" type="text/css" rel="stylesheet"/>
    </head>
    <body>
        <?php if ($ok) { ?>
        <script type="text/javascript">
            window.location = "<?php echo $retPage; ?>";
        </script>
        <?php } else { ?>
        <h1>ERROR!</h1>
        <h3>Post data:</h3>
        <pre><?php print_r($_POST); ?></pre>
        <?php } ?>
    </body>
</html>
